//
//  YSGHelpCenterQuestionListController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/11/19.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

class YSGHelpCenterQuestionListController: BaseViewController {
    
    private var viewModel: YSGHelpCenterQuestionViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    private let isSearch: Bool
    private let code: String
    private let name: String
    
    init(code: String = "", name: String = "", isSearch: Bool) {
        self.code = code
        self.name = name
        self.isSearch = isSearch
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if isSearch {
            navigationItem.titleView = searchBar
        } else {
            title = name
            
            loadData(code: code)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        searchBar.becomeFirstResponder()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        searchBar.resignFirstResponder()
    }
    
    private lazy var searchBar: UISearchBar = {
        let searchBar = UISearchBar()
        searchBar.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
        searchBar.barTintColor = .white
        searchBar.placeholder = "搜索问题"
        searchBar.tintColor = .textDark
        searchBar.setBackgroundImage(UIImage(color: .white), for: .any, barMetrics: .default)
        searchBar.delegate = self
        return searchBar
    }()
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.estimatedRowHeight = 44
            tableView.rowHeight = UITableView.automaticDimension
            tableView.keyboardDismissMode = .onDrag
            tableView.backgroundColor = .background
            tableView.tableFooterView = UIView()
            tableView.register(YSGHelpCenterQuestionCell.nibForCell(),
                               forCellReuseIdentifier: YSGHelpCenterQuestionCell.reuseIdentifier())
        }
    }
}

extension YSGHelpCenterQuestionListController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel?.numberOfSection ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YSGHelpCenterQuestionCell.reuseIdentifier(), for: indexPath) as! YSGHelpCenterQuestionCell
        if let vm = viewModel?.viewModel(at: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
}

extension YSGHelpCenterQuestionListController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 16.0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        guard let vm = viewModel?.viewModel(at: indexPath) else { return }
        
        pushWebVc(with: vm)
    }
}

extension YSGHelpCenterQuestionListController: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        loadData(searchBar.text!)
    }
}

private extension YSGHelpCenterQuestionListController {
    func loadData(_ questionName: String = "", code: String = "") {
        HUDTool.showActivity()
        YSGHelpCenterQuestionViewModel.getQuestionList(type: code, tital: questionName) { (models, message, code) in
            HUDTool.hideActivity()
            switch code {
            case .failure:
                HUDTool.showError(message: message)
            case .success:
                self.viewModel = YSGHelpCenterQuestionViewModel(models: models)
            }
        }
    }
    
    func pushWebVc(with vm: YSGHelpCenterQuestionCellViewModel) {
        let webVc = ReportWebViewController(htmlStr: vm.model.content ?? "", webType: .helpQuestion)
        navigationController?.pushViewController(webVc, animated: true)
    }
}
